package cn.lavenliu.lombokdemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import cn.lavenliu.lombokdemo.entity.Customer;
import cn.lavenliu.lombokdemo.repository.CustomerRepository;

@SpringBootApplication
public class DemoApplication {

	private static final Logger log = LoggerFactory.getLogger(DemoApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Bean
	public CommandLineRunner demo(CustomerRepository customerRepository) {
		return (args) -> {
			// create some customers
			customerRepository.save(new Customer("帝都", "BeiJing"));
			customerRepository.save(new Customer("魔都", "Shanghai"));
			customerRepository.save(new Customer("经济中心", "Shanghai"));
			customerRepository.save(new Customer("天堂", "苏杭"));

			// fetch all customers
			log.info("Customers found with findAll():");
			log.info("-------------------------------");
			for (Customer customer : customerRepository.findAll()) {
				log.info(customer.toString());
			}
			log.info("END");

			// fetch an individual customer by ID
            customerRepository.findById(1L).ifPresent(customer -> {
                log.info("Customer found with findById(1L):");
                log.info("--------------------------------");
                log.info(customer.toString());
				log.info("END");
			});
			
			// fetch customers by last name
			log.info("Customer found with findByLastName('Shanghai'):");
			log.info("--------------------------------------------");
			customerRepository.findByLastName("Shanghai").forEach(shanghai -> {
				log.info(shanghai.toString());
			});
			log.info("END");
		};
	}

}
